//
//  RankingListView.m
//  YanMusicPlayer
//
//  Created by 1564335 on 15-6-13.
//  Copyright (c) 2015年 胡岩 All rights reserved.
//

#import "RankingListView.h"
#import "RankingListCollectionView.h"
#import "RankingListCollectionViewCell.h"
#import "RankingListViewModel.h"

@interface RankingListView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) RankingListCollectionView *rankingListCollectionView;

@property(nonatomic, strong) NSMutableArray *arrayData;

@property(nonatomic, assign) CGPoint startPoint;

@property(nonatomic, strong) NSString *rank_Id;

@property(nonatomic, strong) UIActivityIndicatorView *activity;

@end

@implementation RankingListView

- (instancetype)initWithFrame:(CGRect)frame rank_Id:(NSString *)rank_Id;
{
    _rank_Id = rank_Id;
    _arrayData = [NSMutableArray array];
    self = [super initWithFrame:frame];
    if (self) {
        _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [self addSubview:_activity];
        [_activity startAnimating];
        _activity.center = self.center;
        [self downloadData];
    }
    return self;
}

- (void)downloadData
{
    NSString *path = [[URLPath RankingListURL] stringByAppendingFormat:@"%@&size=50", _rank_Id];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
//        NSLog(@"%@", array);
        for (NSDictionary *dic in array) {
            RankingListViewModel *model = [[RankingListViewModel alloc] initWithDic:dic];
            [self.arrayData addObject:model];
        }
        [self initCollectionView];
        [_activity stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [_activity stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败!" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

- (void)initCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((_WIDTH - 80) / 3.0, (_WIDTH - 80) / 3.0 * 2);
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 15;
    _rankingListCollectionView = [[RankingListCollectionView alloc] initWithFrame:CGRectMake(20, 0, self.frame.size.width - 40, self.frame.size.height) collectionViewLayout:layout];
    _rankingListCollectionView.delegate = self;
    _rankingListCollectionView.dataSource = self;
    _rankingListCollectionView.showsHorizontalScrollIndicator = NO;
    _rankingListCollectionView.showsVerticalScrollIndicator = NO;
    _rankingListCollectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:_rankingListCollectionView];
    [_rankingListCollectionView registerClass:[RankingListCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    RankingListViewModel *model = [_arrayData objectAtIndex:indexPath.row];
    [self.rankDelegate selectIndex:indexPath.row selectId:model.rankList_Id selectTitle:model.title];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RankingListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    RankingListViewModel *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell setRankingListViewModel:model];
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayData.count;
}

@end
